-- Timebomb
-- Created by dengc Oct/11/2016
-- 定时炸弹

-- 初始化
function initTimebomb(self)
    local class = self.gridData.class;
    local pos = self.gridData:getPos();
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            -- 先判断能否点击
            if not self.gridData:canHit() then
                alert(getLocStr("cannot_reach"));
                return;
            end

            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 确定按钮回调，确定按钮文本
            local okFuncList = {};
            local okTextList = {};
            for i = 1, 3 do
                -- 回调
                local function okFunc()
                    DungeonActionM.go("try_remove_time_bomb", pos, i);
                end

                -- 文本
                local text = getLocStr("avalon_time_bomb_ok_text" .. i);

                table.insert(okFuncList, okFunc);
                table.insert(okTextList, text);
            end

            -- 建筑描述
            local dialog = BuildingBonusM.getBuildingDialog(self.gridData);
            dialog = string.format(dialog, AvalonFortM.getBombTime(self.gridData));

            -- 绘制图片的委托函数
            local function drawIconFunc(self, iconImg)
                -- 获取配置
                local config = self.gridData.bomb_vitaport;

                local node = cc.CSLoader:createNode("layout/dungeon/special/TimeBomb.csb");
                self.CT:addChild(node);
                iconImg:loadTexture(getTiemBombPath("time_bomb"));
                iconImg:setOpacity(0);

                node:setPositionY(iconImg:getPositionY() + 15);

                -- 炸弹
                local icon = findChildByName(node, "time_bomb");
                icon:loadTexture(getTiemBombPath("time_bomb"));

                -- 计数器
                local counter = findChildByName(node, "counter");
                counter:loadTexture(getTiemBombPath("counter"));

                -- 计数器的值
                local valueLabel = findChildByName(node, "value");
                TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_RED, false, -1);
                valueLabel:setString(AvalonFortM.getBombTime(self.gridData));

                -- 顺序
                local directionList = {"left", "mid", "right"};

                -- 绘制
                for value, color in pairs(config) do
                    local direction = directionList[value];
                    local line = findChildByName(node, direction);
                    line:loadTexture(getTiemBombPath(direction .. "_" .. color));
                end

                return node;
            end

            -- 打开建筑查看界面
            AudioM.playFx("ui_open1");
            self:openBuildingView5(
                okFuncList,
                okTextList,
                nil,
                dialog,
                nil,
                drawIconFunc,
                {30, 60, 70, -15, -15}
            );
        end
    end

    -- 使用一张透明的图片来接收点击事件
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:setOpacity(0);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.item:setPositionY(8);

    -- 绘制定时炸弹
    redrawTimeBomb(self);
end

-- 拆除定时炸弹
function onRemoveTimeBomb(self, args)
    local isSucc = args["isSuccess"];
    local pos    = args["pos"];

    -- 弹出提示
    if isSucc then
        -- 成功
        alert(getLocStr("avalon_time_bomb_succ"));

        -- 建筑物奖励事件
        local bonus = args["bonus"];
        if bonus then
            self:onBuildingBonus({["bonus"] = bonus}, nil, nil, true);
        end
    else
        -- 失败
        alert(getLocStr("avalon_time_bomb_fail"));

        -- 爆炸光效
        playBuildingDisappearEffect(self, "bomb");

        -- 建筑消失
        playBuildingDisappear2(self);
    end
end

-- 绘制定时炸弹
function redrawTimeBomb(self)
    local node = cc.CSLoader:createNode("layout/dungeon/special/TimeBomb.csb");
    self.item:addChild(node);
    node:setName("timebombNode");

    node:setPositionY(node:getPositionY() + 15);

    -- 炸弹
    local icon = findChildByName(node, "time_bomb");
    icon:loadTexture(getTiemBombPath("time_bomb"));

    -- 计数器
    local counter = findChildByName(node, "counter");
    counter:loadTexture(getTiemBombPath("counter"));

    -- 计数器的值
    local valueLabel = findChildByName(node, "value");
    TextStyleM.setTextStyle(valueLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_RED, false, -1);
    valueLabel:setString(AvalonFortM.getBombTime(self.gridData));

    -- 顺序
    local directionList = {"left", "mid", "right"};

    -- 获取配置
    local config = self.gridData.bomb_vitaport;

    -- 从左到右依次随机颜色
    for i = 1, #directionList do
        local direction = directionList[i];
        local color = config[i];

        local line = findChildByName(node, direction);

        line:loadTexture(getTiemBombPath(direction .. "_" .. color));
    end
end

-- 更新定时炸弹的剩余时间
function onUpdateTimeBombTime(self)
    local node = findChildByName(self.item, "timebombNode");
    if not node then
        return;
    end

    -- 更新计数器的值
    local valueLabel = findChildByName(node, "value");
    valueLabel:setString(AvalonFortM.getBombTime(self.gridData) - 1);
end
